package com.xiumiing.net.base.net.interceptor;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.CrashUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PhoneUtils;
import com.blankj.utilcode.util.SPUtils;


import java.io.IOException;

import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;


/**
 * @author Created by Jeff
 * on 2019/6/17.
 * des:业务拦截
 */
public class HeaderInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        //从新设置 url （做接口版本控制）
//                HttpUrl newUrl = setUrl(request);
//                Request.Builder builder = request.newBuilder().url(newUrl);
        Request.Builder builder = request.newBuilder();
        //添加 请求 Header
        addHeader(builder);

        Response response;
        try {
            response = chain.proceed(builder.build());
        } catch (Exception e) {
            LogUtils.e("HTTP", "<-- HTTP FAILED: " + e);
            throw e;
        }
        //请求结果保存用户 UserInfoKey
        saveUserInfoKey(response);
        return response;
    }

    private void saveUserInfoKey(Response response) {
//        if (EmptyUtils.isEmpty(response)) return;
//        String responseUrl = EmptyUtils.isEmpty(response.request().url()) ? "" : response.request().url().toString();
//        Headers headers = response.headers();
//        String app_user_info_key = headers.get("app-user-info-key");
//        if (EmptyUtils.isNotEmpty(app_user_info_key)) {
//            // 登录接口返回key保存
//            if (responseUrl.contains("auth/accredit/login")
//                    || responseUrl.contains("auth/password/login")
//                    || responseUrl.contains("auth/cellphone/login")) {
//                UserHelper.saveLoginKey(app_user_info_key);
//            }
//        }
    }

    private void addHeader(Request.Builder builder) {
//        builder.addHeader("Connection", "close");
//        builder.addHeader(USER_INFO_KEY, UserHelper.getLoginKey());
//        builder.addHeader(Constants.YNS_CHANELSOURCE, "Android");
//        builder.addHeader(Constants.YNS_APP_VERSION, BuildConfig.VERSION_NAME);
//        String requestId = YNDoSecretUtils.createRandomCharData(9);
//        builder.addHeader(Constants.REQUESTID, requestId);
//        builder.addHeader(Constants.SIGN, YNDoSecretUtils.getMd5Sign(requestId));
//        builder.addHeader(Constants.DEVICE_INFO, CrashUtils.DEVICE_INFO);
//        String imei = SPUtils.getInstance().getString(Constants.IMEI);
//        if (EmptyUtils.isEmpty(imei)) {
//            try {
//                imei = PhoneUtils.getDeviceId();
//                SPUtils.getInstance().put(Constants.IMEI, imei);
//            }catch (Exception e){}
//        }
//        builder.addHeader(Constants.IMEI, imei);
//        builder.addHeader(Constants.DEVICE, "Android");
    }

//    @NonNull
//    private HttpUrl setUrl(Request request) {
////                //http://127.0.0.1/test/upload/img?userName=xiaoming&userPassword=12345
////                String scheme = url.scheme();//  http https
////                String host = url.host();//   127.0.0.1
////                String path = url.encodedPath();//  /test/upload/img
////                String query = url.encodedQuery();//  userName=xiaoming&userPassword=12345
//        HttpUrl httpUrl = request.url();
//        String encodedPath = httpUrl.encodedPath();
//        String replace = encodedPath.replace("api/v1", "api/" + API_VERSION);
//        return request.url().newBuilder()
//                .encodedPath(replace)
//                .build();
//    }
}
